﻿--[[
   @brief    control for wegame mainframe
   @author   hqzheng
   @version  1.0
   @date     2017-04-12
--]]

pwg_social_user_guide = {

    kGuidePageWidth = 534;
    kGuidePageHeight = 647;

    kGuidePageRecommendOrg = 1;
    kGuidePageNewSocial = 2;
};

wegame_mainframe_control = 
{
    cur_color_mode_ = nil;
    downloader_tips_type = "";
    downloader_game_name = "";
    downloader_game_id = 0;
    web_notify_is_login_ = false;
    web_notify_is_register_event_ = false;
    web_notify_is_done_ = false;
    has_preloaded_json_cfg_for_main_web_ = false;
    first_page_param = nil;
    first_focus_param = nil;
    has_already_navigate = false;
    bNeedNotifyWebRefreshPage = false;
    mygame_list_ready = false;
    new_social_init_org_list = {};
    pwg_social_enabled = true;

    destroy_when_close_wnd_ = true;
    notify_web_init_when_show_ = false;

    init = function ()
        ierd_tgp.tpf_ui.Require("wegame_mainframe\\model.lua");
        ierd_tgp.tpf_ui.Require("wegame_mainframe\\view.lua");
        ierd_tgp.tpf_ui.Require("wegame_mainframe\\ping_helper.lua");

        if base_utility.get_client_version_type() == base_utility.CLIENT_TYPE.OVERSEA_VER then
            wegame_mainframe_control.pwg_social_enabled = false;
        end

        TPingHelper.Init();

        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        if nil ~= tpfui then
              tpfui:AddLuaListener(ierd_tgp.game_library.SHOW_MAIN_WND, "wegame_mainframe_control.show_main_wnd");
              tpfui:AddLuaListener(ierd_tgp.game_library.CLOSE_MAIN_WND, "wegame_mainframe_control.close_main_wnd");
              tpfui:AddLuaListener(ierd_tgp.game_library.SAVE_MAIN_WND_POS, "wegame_mainframe_control.save_main_wnd_pos");
--            tpfui:AddLuaListener(ierd_tgp.game_library.MSG_NOTIFY, "wegame_mainframe_control.on_msg_notify");
              tpfui:AddLuaListener(ierd_tgp.game_library.DOWNLOADER_TIPS, "wegame_mainframe_control.on_downloader_tips");
              tpfui:AddLuaListener(ierd_tgp.game_library.JSON_CFG_UPDATED, "wegame_mainframe_control.on_get_json_cfg");
              tpfui:AddLuaListener(ierd_tgp.game_library.LAUNCH_WEB_GAME_USE_QB_BROWSER, "mainframe_control.launch_web_use_qb_browser");
        end

        comm_center.listen_broadcast("i_broadcast_nav_tab_switch", wegame_mainframe_control.handle_nav_tab_switch);
        comm_center.listen_broadcast("i_broadcast_sys_setting_close_main_wnd_oper", wegame_mainframe_control.handle_close_main_wnd_oper);
        comm_center.listen_broadcast("i_broadcast_show_user_guide", wegame_mainframe_control.handle_show_user_guide);
        comm_center.listen_broadcast("Msg_MainWndShow", "wegame_mainframe_control.handle_mainframe_first_show");
        comm_center.listen_broadcast("Msg_WeGame_Auto_Exit", wegame_mainframe_control.auto_exit_main_process);
        comm_center.listen_broadcast("i_broadcast_on_bottom_bar_show", wegame_mainframe_control.handle_bottom_bar_show);
        comm_center.listen_broadcast("i_broadcast_common_cfg_updated", "wegame_mainframe_control.on_common_cfg_updated");
        comm_center.listen_broadcast("Msg_UserSelfRegionNotify",
         "wegame_mainframe_control.handle_userself_region_notify");
        comm_center.listen_broadcast("i_broadcast_page_load_complete", wegame_mainframe_control.on_load_page_complete);
        
        event_center.add_listener(web_register_event.REGISTER_SUCCESS_EVENT, "wegame_mainframe_control.handle_register_event");
        comm_center.reg_service("i_svr_focus_item", "wegame_mainframe_control.handle_focus_item");
        comm_center.reg_service("i_svr_is_vip", "wegame_mainframe_control.handle_req_is_vip");
        comm_center.reg_service("i_svr_save_main_wnd_pos", "wegame_mainframe_control.save_main_wnd_pos");
        comm_center.reg_service("i_svr_is_preupdate_rule_open", "wegame_mainframe_control.get_preupdate_rule_open");
        comm_center.reg_service("SvrChangeRegionSuccNotify", 
            "wegame_mainframe_control.handle_change_region_succ_notify");
        comm_center.reg_service("SvrGetCurrentNavTab",
            wegame_mainframe_control.handle_get_current_nav_tab);
        comm_center.reg_service("i_service_new_social_query_init_org_list",
                                "wegame_mainframe_control.query_new_social_init_org_list_svr");
        comm_center.reg_service("i_service_check_pwg_social_state", 
                                "wegame_mainframe_control.on_check_pwg_social_state");
        comm_center.listen_broadcast("MsgMyGameListUpdate",
                                     "wegame_mainframe_control.on_mygame_list_update");
        comm_center.listen_broadcast("MsgTGPConfigMgr_UpdateClientMode",
                                     "wegame_mainframe_control.on_color_mode_update");
    end;

    inner_log = function (msg, core_msg)
        base_utility.log(string.format("[wegame_mainframe_control] %s", msg), core_msg);
    end;

    show_main_wnd = function(event_id, json_str)
        wegame_mainframe_control.inner_log("show_main_wnd, " .. json_str);
        jtable = base_utility.json_to_table(json_str);

        if base_utility.is_big_picture_ui() then
            comm_center.call_service("i_service_show_bp_wnd", {show=1});
            return;
        end

        if true == wegame_mainframe_model.first_show then
            local width_info = {common = 0, key = "wegame_mainframe.wnd_width"};
            local width_data = comm_center.call_service("Svr_GetConfig", width_info);
            local height_info = {common = 0, key = "wegame_mainframe.wnd_height"};
            local height_data = comm_center.call_service("Svr_GetConfig", height_info);
            local max_info = {common = 0, key = "wegame_mainframe.wnd_max_status"};
            local max_data = comm_center.call_service("Svr_GetConfig", max_info);
            local close_main_wnd_info = {common = 0, key = "CfgMgr.UserSysSetting.CloseMainWndOper"};
            local close_main_wnd_data = comm_center.call_service("Svr_GetConfig", close_main_wnd_info);
            local device_xone = comm_center.call_service("Svr_LoginMgr_IsDeviceXOne");
            local is_device_xone = false;
            if device_xone ~= nil and device_xone.is_xone ~= nil and device_xone.is_xone~= 0 then
                is_device_xone = true;
            end;

            if 0 == close_main_wnd_data.exits then
                wegame_mainframe_model.exit_when_close = jtable["exit_when_close"];
            else
                wegame_mainframe_model.exit_when_close = tonumber(close_main_wnd_data.value);
            end

            if "" ~= width_data.value and "" ~= height_data.value then
                wegame_mainframe_model.save_size = 
                    {width = tonumber(width_data.value),
                        height = tonumber(height_data.value)};
                wegame_mainframe_model.has_size_record = true;
            end
            if wegame_mainframe_model.should_launch_max_mode() or 1 == tonumber(max_data.value) or
               is_device_xone == true then
                wegame_mainframe_model.save_size["is_max"] = true;
            else 
                wegame_mainframe_model.save_size["is_max"] = false;
            end
        end

        if wegame_mainframe_view.show(wegame_mainframe_model.first_show) == true then
            local param = {};
            param.first_show = wegame_mainframe_model.first_show;
            event_center.send_event(mainframe_event.MAINFRAME_WND_SHOWN, base_utility.table_to_json(param));  
            if false == wegame_mainframe_model.is_notify_init_game_list then
                local input_data = {isfirst = 1};
                comm_center.send_broadcast("Msg_Show_BannerList", input_data);
                wegame_mainframe_model.is_notify_init_game_list = true;
            end
        end;

        wegame_mainframe_control.start_waiting_web_mainframe();
       
        wegame_mainframe_model.first_show = false;
    end;

    handle_mainframe_first_show = function(param_tbl)
        if param_tbl ~= nil and param_tbl.isfirst == 1 then
            local game_lib =
                base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
            if game_lib ~= nil then
                game_lib:get_json_cfg("defualt_open_page_setting");
                game_lib:get_json_cfg("navigate_tab_menu_setting");
                game_lib:get_json_cfg("top_bar_skin");
                game_lib:get_json_cfg("preupdate_rule");
                game_lib:get_json_cfg("app_tips_config");
            end;

            if wegame_mainframe_model.is_start_for_login or 
                wegame_mainframe_model.current_select_modure_id == GAME_ID.GAEM_ID_HOME_PAGE then
                wegame_mainframe_view.set_list_panel_visible(true);
                if false == wegame_mainframe_control.process_game_list_pack_up() then
                    -- 默认展开
                    wegame_mainframe_view.set_pack_up_btn_backgroup(wegame_mainframe_model.current_select_modure_id);
                    wegame_mainframe_view.pack_up_game_list(false);
                    wegame_mainframe_view.adjust_helper_bottom_bar_size();
                end
                
                local tab_info = {
                    tab_type = GAME_ID.GAEM_ID_HOME_PAGE,
                };
                comm_center.call_service("i_service_switch_tab", tab_info);
            else
                wegame_mainframe_view.set_list_panel_visible(false);
                wegame_mainframe_view.set_pack_up_btn_visible(false);
            end
            wegame_mainframe_control.handle_frame_load_complete();
            wegame_mainframe_control.focus_first_login_banner();
            wegame_mainframe_control.query_fn_authority();
            wegame_mainframe_control.query_user_org_guide_config();
            wegame_mainframe_control.init_destroy_strat();
        end
    end;

    -- 恢复窗口
    restore_main_wnd = function()
        local show_wnd_type_info = comm_center.call_service("SvrShowMainWndOrLoginWnd", {});
        if nil ~= show_wnd_type_info and 1 == show_wnd_type_info.show_wnd_type then
            wegame_mainframe_view.show(false);
        end
    end;

    close_main_wnd = function(event_id, json_str)
        wegame_mainframe_control.inner_log("close_main_wnd");
        wegame_mainframe_view.close_main_window();
    end;

    on_js_callback = function(event_id, json_param)
        local param = base_utility.json_to_table(json_param);
        local func_name = param.func_name;
        local json = param.json;
        wegame_mainframe_control.inner_log("on_js_callback.func_name="..func_name);
        if func_name == "jc_set_app_status" then
            wegame_mainframe_control.inner_log("on_js_callback set_app_status:" .. json);
            wegame_mainframe_control.handle_set_app_status(json);
        elseif func_name == "jc_check_app_status" then
            local param_table = base_utility.json_to_table(json);
            wegame_mainframe_control.handle_check_app_status(param_table);
        elseif func_name == "jc_get_window_state" then
            wegame_mainframe_control.handle_get_window_state(json);
        elseif func_name == "jc_save_history" then
            wegame_mainframe_control.handle_save_jump_history(json);
        elseif func_name == "jc_go_back" then
            wegame_mainframe_control.handle_go_back(json);
        elseif func_name == "jc_show_dlc_btn" then
            local param_table = base_utility.json_to_table(json);
            local show_dlc_btn = false;
            if param_table.hasDlcList ~= nil and param_table.hasDlcList == true then
                show_dlc_btn = true;
            end;

            base_utility.log("[wegame_mainframe_control] show_dlc_btn = ".. tostring(param_table.hasDlcList));
            frame_bottom_view.show_dlc_btn(show_dlc_btn);
        elseif func_name == "jc_web_move_animation_end" then
            wegame_mainframe_control.handle_web_move_animation_end(json);
        elseif func_name == "jc_get_page_cache_switch" then
            wegame_mainframe_control.handle_get_page_cache_switch(json);
        end;
    end;

    handle_set_app_status = function(json)
        local param_table = base_utility.json_to_table(json);
        local game_id = nil;
        if param_table ~= nil and param_table.data ~= nil then
            game_id = param_table.data.game_id;
        end
        if game_id == nil then return end;
        local event_table = {module_id = game_id};
        event_table.callback_func = param_table.callback_func;
        event_table.src = param_table.src;
        if param_table.seq_num ~= nil then
            event_table.seq_num = param_table.seq_num;
        end
        local module_info = banner_model.get_banner_module_info_by_game_id(tonumber(game_id));
        if module_info ~= nil then
            event_table.module_url = module_info.module_url;
        end
        
        event_table.attach_info = wegame_mainframe_control.get_selected_attach_info(tonumber(game_id));

        event_center.send_event(banner_event.FOCUS_BANNER_ITEM, base_utility.table_to_json(event_table));
    end;

    get_selected_attach_info = function(game_id)
        if game_id == nil or game_id == 0 or tonumber(game_id) == 0 then
            return;
        end;
        
        local attach_info = comm_center.call_service("Svr_CheckHasAttachRel", 
                           {game_id = tonumber(game_id), has_attach = 0, select_id = 0, main_game_id = 0});
        if attach_info == nil or attach_info.has_attach ~= 1 or 
           attach_info.select_id == 0 or attach_info.main_game_id == 0 then
            return;
        end;

        local ret = {};
        ret.main_game_id = attach_info.main_game_id;
        ret.select_id = attach_info.select_id;
        local module_info = banner_model.get_banner_module_info_by_game_id(attach_info.select_id);
        if module_info ~= nil then         
            ret.select_url = module_info.module_url;
        end
        return ret;
    end;

    handle_check_app_status = function(param_table)
        if param_table ~= nil and param_table.data ~= nil then
            local game_id = param_table.data.game_id;
            if game_id == nil then return end;
            
            local result = banner_control.get_game_visible_by_gameid(
                tonumber(game_id));
            local retCode  = 0;
            if result == true  then
                retCode = 0;
            else  
                retCode = 1;
            end
            local rsp_table = web_utility.web_rsp_comm_data(param_table, retCode, "");
            rsp_table.mid = game_id;
            if param_table.callback_func ~= nil and param_table.callback_func ~= "" then
                local rsp_json = base_utility.table_to_json(rsp_table);
                web_utility.lua_call_js_ex(param_table.callback_func, rsp_json, CID_MAINFRAME_QBWEB);
                base_utility.log("[web_mainframe_control] handle_check_app_status, data:"..rsp_json);
            end
        end
    end;

    handle_get_window_state = function(json_str)
        local param_table = base_utility.json_to_table(json_str);
        local rsp_table = web_utility.web_rsp_comm_data(param_table, 0, "");
        rsp_table.data = {window_state = wegame_mainframe_view.framestate};   
        if param_table.callback_func ~= nil and param_table.callback_func ~= "" then
            local rsp_json = base_utility.table_to_json(rsp_table);
            web_utility.lua_call_js_ex(param_table.callback_func, rsp_json, CID_MAINFRAME_QBWEB);
            base_utility.log("[wegame_mainframe_control] handle_get_window_status, data:"..rsp_json);
        end
    end;

    handle_get_page_cache_switch = function(json_str)
        local param_table = base_utility.json_to_table(json_str);
        local rsp_table = web_utility.web_rsp_comm_data(param_table, 0, "");
        rsp_table.data = {cache_switch = wegame_mainframe_model.page_cache_switch};   
        if param_table.callback_func ~= nil and param_table.callback_func ~= "" then
            local rsp_json = base_utility.table_to_json(rsp_table);
            web_utility.lua_call_js_ex(param_table.callback_func, rsp_json, CID_MAINFRAME_QBWEB);
            base_utility.log(
                "[wegame_mainframe_control] handle_get_page_cache_switch, data:"..rsp_json);
        end
    end;

    handle_save_jump_history = function (json_str)
        local param_table = base_utility.json_to_table(json_str);
        if nil ~= param_table.data then
            wegame_mainframe_view.save_jump_url(param_table.data.game_id, param_table.data.url);
        end
    end;

    handle_go_back = function (json_str)
        web_operation.go_back();
    end;

    json2_module_id = function (json_str)
        local param = base_utility.json_to_table(json_str);
        local module_id = 0;
        if param ~= nil and param.module_id ~= nil then
            module_id = param.module_id;
        else
            wegame_mainframe_control.inner_log("json2_module_id, param is nil");
        end
    
        return module_id;
    end;

    -- 跳转模块对应的页面
    jump_module_page = function (event_id, json_str)
        wegame_mainframe_control.inner_log("jump_module_page");
        local module_id = wegame_mainframe_control.json2_module_id(json_str);
        local tab_info = {};
        if GAME_ID.GAME_ID_GAME_LIVE == tonumber(module_id) then
            tab_info.tab_type = GAME_ID.GAME_ID_GAME_LIVE;
            wegame_mainframe_view.set_list_panel_visible(false);
            wegame_mainframe_view.set_pack_up_btn_visible(false);
        elseif GAME_ID.GAME_ID_SHOP == tonumber(module_id) then
            tab_info.tab_type = GAME_ID.GAME_ID_SHOP;
            wegame_mainframe_view.set_list_panel_visible(false);
            wegame_mainframe_view.set_pack_up_btn_visible(false);
        elseif GAME_ID.GAME_ID_NETBAR == tonumber(module_id) then
            tab_info.tab_type = GAME_ID.GAME_ID_NETBAR;
            wegame_mainframe_view.set_list_panel_visible(false);
            wegame_mainframe_view.set_pack_up_btn_visible(false);
        elseif GAME_ID.GAME_ID_TESTZONE == tonumber(module_id) then
            tab_info.tab_type = wegame_mainframe_model.nav_tab_type.TAB_TESTZONE;
            wegame_mainframe_view.set_list_panel_visible(false);
            wegame_mainframe_view.set_pack_up_btn_visible(false);
        elseif GAME_ID.GAME_ID_CHINATOWN == tonumber(module_id) then
            tab_info.tab_type = GAME_ID.GAME_ID_CHINATOWN;
            wegame_mainframe_view.set_list_panel_visible(false);
            wegame_mainframe_view.set_pack_up_btn_visible(false);
        else
            tab_info.tab_type = GAME_ID.GAEM_ID_HOME_PAGE;
            if false == wegame_mainframe_model.is_game_list_pack_up then
                wegame_mainframe_view.set_list_panel_visible(true);
            else
                wegame_mainframe_view.set_list_panel_visible(false);
                wegame_mainframe_view.set_pack_up_btn_visible(true);
                wegame_mainframe_view.adjust_pack_up_btn_position();
                wegame_mainframe_view.adjust_helper_bottom_bar_size();
            end
        end
        comm_center.call_service("i_service_switch_tab", tab_info);
        local module_info = banner_model.get_banner_module_info_by_game_id(module_id);
        if nil ~= module_info then
            wegame_mainframe_view.set_module_color(module_info.module_color);
        end
    end; 

    handle_nav_tab_switch = function (data)
        if nil == data or nil == data.nav_tab_type then
            return;
        end

        if GAME_ID.GAME_ID_GAME_LIVE == data.nav_tab_type or 
            GAME_ID.GAME_ID_SHOP == data.nav_tab_type or 
            GAME_ID.GAME_ID_NETBAR == data.nav_tab_type or 
            GAME_ID.GAME_ID_TESTZONE == data.nav_tab_type then
            wegame_mainframe_view.set_list_panel_visible(false);
            wegame_mainframe_view.set_pack_up_btn_visible(false);
        else
            if false == wegame_mainframe_model.is_game_list_pack_up then
                wegame_mainframe_view.set_list_panel_visible(true);
            end
            if false == wegame_mainframe_model.is_notify_init_game_list then
                local input_data = {isfirst = 1};
                comm_center.send_broadcast("Msg_Show_BannerList", input_data);
                wegame_mainframe_model.is_notify_init_game_list = true;
            end
            if true == wegame_mainframe_control.mygame_list_ready then
                comm_center.call_service("Svr_UpdateSubscriptionList", {});
            end
        end

        if nil ~= data.moudle_id then
            wegame_mainframe_model.current_select_modure_id = data.moudle_id;
        end
    end;
    
    -- 和框架通信专用，框架初始化的第一个交互接口
    web_mainframe_request_first_info = function(param)
        wegame_mainframe_control.inner_log("web_mainframe_request_first_info");
        if nil ~= param then
            local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
            -- 登录态票据
            local web_st = "";
            local pkey_str = "";
            local tgp_ticket = "";
            local biz_ticket = "";
            local geoid = 0;
            local lcid = 0;

            local login_mgr =
                 base_utility.get_component_adapter("ILogin_mgr",
                     "ierd_tgp.login_mgr.GetLoginMgr();");
            if login_mgr ~= nil then
                local info = ierd_tgp.login_mgr.WebStPod:new();
                login_mgr:GetWebSt(info);
                web_st = info.web_st;
                info:delete();
                local client_st_info = ierd_tgp.login_mgr.ClientStPod:new();
                login_mgr:GetClientSt(client_st_info);
                pkey_str = client_st_info.client_st;
                client_st_info:delete();
                local tgp_ticket_info = ierd_tgp.login_mgr.TGPTicketPod:new();
                login_mgr:GetTGPTicket(tgp_ticket_info);
                tgp_ticket = tgp_ticket_info.tgp_st;
                tgp_ticket_info:delete();
                geoid = login_mgr:GetGeoID();
                lcid = login_mgr:GetLCID();
                local biz_ticket_info = ierd_tgp.login_mgr.TGPTicketPod:new();
                login_mgr:GetBizTicket(biz_ticket_info);
                biz_ticket = biz_ticket_info.tgp_st;
                biz_ticket_info:delete();
            end

            -- 帐号ID
            local self_tgp_id = TLoginModule.GetTGPID();
            local current_uin = TLoginModule.GetLoginUin();

            -- 版本信息
            local ver = 0;
            local netbar_flag = 0;
            local dev_version = false;
            local str = NewStr(260);
            local game_lib =
                base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
            if nil ~= game_lib then
                netbar_flag = game_lib:get_bind_channel_id();
                dev_version = game_lib:get_tgp_version(str, 260, dev_version);
                ver = game_lib:get_tgp_version_num();
            end;

            -- 获取登录类型和服务器环境参数
            local login_account_type = TLoginModule.GetLoginAccountType();
            local env_value = "";
            local env_tbl = comm_center.call_service("Srv_GetLoginEnvInfo", {});
            if env_tbl ~= nil and env_tbl.server_env ~= nil then
                env_value = env_tbl.server_env;
            end
            
            -- 跳转节点信息，修正跳转参数
            if wegame_mainframe_control.first_page_param == nil then
                wegame_mainframe_control.first_page_param = wegame_mainframe_control.get_first_page_param();
            end;
            local info = wegame_mainframe_control.get_first_page_module_info(wegame_mainframe_control.first_page_param);
            wegame_mainframe_control.inner_log("jump module info: "..base_utility.table_to_json(info));

            -- 颜色模式
            local color_mode = base_utility.get_color_mode();

            rsp_table.data = {
                web_st = web_st, pkey = pkey_str, tgp_ticket = tgp_ticket, tgp_biz_ticket = biz_ticket,
                uin = current_uin, tgp_id = self_tgp_id, 
                dev_client = dev_version,
                is_netbar = netbar_flag, version = ver,
                tgp_env = env_value, tgp_user_type = login_account_type,
                module_info = info, geoid = geoid, lcid = lcid, client_type = client_type,
                color_mode = color_mode};
            if param.callback_func ~= nil and param.callback_func ~= "" then
                local rsp_json = base_utility.table_to_json(rsp_table);
                web_utility.lua_call_js_ex(param.callback_func, rsp_json, CID_MAINFRAME_QBWEB);
            end
            wegame_mainframe_control.first_page_param = nil;
        end;
    end;

    get_first_page_module_info = function(first_page_param)
        wegame_mainframe_control.inner_log("get_first_page_module_info" ..
            base_utility.table_to_json(first_page_param));
        local info = {};
        info.jump_param = {};
        info.jump_info = {};
        if first_page_param.check_attach ~= nil and first_page_param.check_attach == 1 and
           first_page_param.attach_info ~= nil and first_page_param.attach_info.select_id ~= 0 then
            info.jump_info = banner_model.get_web_rsp_module_info(first_page_param.attach_info.select_id);
            if info.jump_info ~= nil then
                info.jump_param.module_id = info.jump_info.game_id;
                info.jump_param.module_url = info.jump_info.module_url;
            else
                wegame_mainframe_control.inner_log("[get_first_page_module_info]jump_info is nil",
                        true);
                info.jump_param.module_id = first_page_param.attach_info.select_id;
                info.jump_param.module_url = "";
            end
        else
            info.jump_param = first_page_param;
            info.jump_info = banner_model.get_web_rsp_module_info(first_page_param.module_id);
            if info.jump_param.module_url == nil or info.jump_param.module_url == "" then
                -- 第一个跳转地址为空时，使用默认配置地址
                if info.jump_info ~= nil then
                    info.jump_param.module_url = info.jump_info.module_url;
                end;
            end
        end;
        return info;
    end;

    -- 和框架通信专用，获取页面框架加载的第一个页面信息 {module_id:必填, module_url:可选}
    get_first_page_param = function()
        wegame_mainframe_control.inner_log("get_first_page_param");
        local param = {};
        if wegame_mainframe_model.jump_module_web_json ~= nil then
            local info = base_utility.json_to_table(wegame_mainframe_model.jump_module_web_json);
            if info ~= nil and info.module_id ~= nil then
                param.module_id = info.module_id;
                param.module_url = info.module_url;
            else
                param.module_id = GAME_ID.GAME_ID_SHOP;
            end
            wegame_mainframe_model.jump_module_web_json = nil;
        else
            local module_id = wegame_mainframe_model.current_select_modure_id;
            if module_id == GAME_ID.GAEM_ID_HOME_PAGE and nil ~= banner_model.last_selected_game_id_ then
                module_id = banner_model.last_selected_game_id_;
            end
            if true == wegame_mainframe_model.has_start_for then
                module_id = wegame_mainframe_model.start_for_module_id;
            else
                param.check_attach = 1;
            end

            if nil == module_id then
                module_id = GAME_ID.GAME_ID_SHOP;
            end
            local module_url = nil;
            if false == wegame_mainframe_model.hase_open_defualt_page and 
               nil ~= wegame_mainframe_model.default_open_page_setting and
               false == wegame_mainframe_model.has_start_for then
                if base_utility.is_netbar_ver() then
                    module_url = wegame_mainframe_model.default_open_page_setting.netbar_module_url; 
                else
                    module_url = wegame_mainframe_model.default_open_page_setting.module_url; 
                end
                wegame_mainframe_model.hase_open_defualt_page = true;
            end
            -- 首次打开后，标志位清掉
            wegame_mainframe_model.has_start_for = false;
            param.module_id = module_id;
            param.module_url = module_url;

            param.attach_info = wegame_mainframe_control.get_selected_attach_info(tonumber(module_id));
        end;
        wegame_mainframe_control.inner_log("first page param: "..base_utility.table_to_json(param));
        return param;
    end;

    preload_json_cfg_for_main_web = function ()
        local game_lib =
                base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
        if game_lib ~= nil then
            game_lib:get_json_cfg("defualt_open_page_setting");
            game_lib:get_json_cfg("wg_banner_config");
        end
    end;
    
    -- 登录成功后定位第一个节点
    focus_first_login_banner = function()
        local game_id = GAME_ID.GAME_ID_SHOP;
        local url = nil;
        if wegame_mainframe_control.first_focus_param ~= nil and 
           wegame_mainframe_control.first_focus_param.module_id ~=nil then
            game_id = wegame_mainframe_control.first_focus_param.module_id;
            url = wegame_mainframe_control.first_focus_param.module_url;
        end;
        if url == nil then
            local module_info = banner_model.get_banner_module_info_by_game_id(game_id);
            if module_info ~= nil and module_info.module_url ~= nil then
                url = module_info.module_url;
            end
        end;
        wegame_mainframe_control.only_focus_banner(game_id, url);
    end;

    -- 定位节点，不加载页面
    only_focus_banner = function(game_id, url)
        local focus_tab = { module_id = game_id, 
                            module_url = url,
                            without_notify_web = true };
        if wegame_mainframe_control.first_focus_param ~= nil and
           wegame_mainframe_control.first_focus_param.check_attach ~= nil and
           wegame_mainframe_control.first_focus_param.check_attach == 1 then
            focus_tab.check_attach = 1;
            focus_tab.attach_info = wegame_mainframe_control.first_focus_param.attach_info;
        end;
        
        event_center.send_event(banner_event.FOCUS_BANNER_ITEM, base_utility.table_to_json(focus_tab));
    end;

    handle_register_event = function(event_id, json_str)
        local param_table = base_utility.json_to_table(json_str);
        if nil == param_table then return end
        if param_table.event_name ~= nil and "plugin_life_cycle_event" == param_table.event_name then
            wegame_mainframe_control.inner_log("listened plugin_life_cycle_event: "..json_str);
            wegame_mainframe_model.web_mainframe_ready = true;
            if wegame_mainframe_control.first_focus_param ~= nil then
                -- 不是首次登录，需要这里定位一下tab
                wegame_mainframe_control.only_focus_banner(param_table.data.module_id, param_table.data.module_url);
            end;
            wegame_mainframe_control.handle_frame_load_complete();
            comm_center.send_broadcast("Msg_WebFrameLoadComplete",{});
        elseif param_table.event_name ~= nil and "web_mainframe_comm_listen" == param_table.event_name then
            wegame_mainframe_control.web_notify_is_register_event_ = true;
            if wegame_mainframe_control.has_preloaded_json_cfg_for_main_web_ == false then -- 只加载一次
                wegame_mainframe_control.has_preloaded_json_cfg_for_main_web_ = true;
                wegame_mainframe_control.preload_json_cfg_for_main_web(); -- 提前加载oss配置文件（本地的离线文件）
            end

            -- 框架第一次加载需要，先缓存起来
            if wegame_mainframe_control.first_page_param == nil then
                wegame_mainframe_control.first_page_param = wegame_mainframe_control.get_first_page_param();
            end;
            -- 客户端顶栏初始化完成定位需要，先缓存起来
            wegame_mainframe_control.first_focus_param = wegame_mainframe_control.first_page_param;

            local info = wegame_mainframe_control.get_first_page_background(wegame_mainframe_control.first_page_param);
            if info ~= nil then
                wegame_mainframe_view.web_notify_to_set_background(info.background_image, 
                                                                   info.background_color,
                                                                   info.image_position);
            end
            if wegame_mainframe_control.notify_web_init_when_show_ == false then
                -- 登录完成后，如果用户最小化再恢复就没法显示web了
                wegame_mainframe_control.web_notify_is_done_ = false; 
                wegame_mainframe_control.web_notify_to_init();
            end;
        end
    end;
    
    get_first_page_background = function(first_page_param)
        local game_id = first_page_param.module_id;
        if first_page_param.check_attach ~= nil and first_page_param.check_attach == 1 and
           first_page_param.attach_info ~= nil and first_page_param.attach_info.select_id ~= 0 then
            game_id = first_page_param.attach_info.select_id;
        end;
        local info = banner_model.get_web_rsp_module_info(game_id);
        return info;
    end;
    
    -- 检查首次登录是否有专版或者是launcher决定的定位节点
    check_first_focus_gameid = function()
        wegame_mainframe_control.inner_log("check_first_focus_gameid");
        local start_for = GAME_ID.INVALID_GAME_ID;
        local info = comm_center.call_service("Svr_GetFirstFocusGameId", {});
        if info ~= nil then
            wegame_mainframe_control.inner_log("Svr_GetFirstFocusGameId, result: "..tostring(info.game_id));
            start_for = info.game_id;
        end;
        if GAME_ID.INVALID_GAME_ID == start_for then
            local quick_start_info = comm_center.call_service("Svr_GetQuickStartGameId", {});
            if quick_start_info ~= nil then
                start_for = quick_start_info.game_id;
            end;
        end

        if GAME_ID.INVALID_GAME_ID == start_for then
            local multi_wegame_info = comm_center.call_service(
                "Svr_GetMultiWeGameLaunchGameID", {});
            if nil ~= multi_wegame_info then
                start_for = multi_wegame_info.game_id;
            end
        end
        if start_for ~= nil and "number" == type(start_for) and GAME_ID.INVALID_GAME_ID ~= start_for then
            wegame_mainframe_model.start_for_module_id = start_for;
            wegame_mainframe_model.has_start_for = true;
            wegame_mainframe_model.is_start_for_login = true;
        end
    end;

    update_start_for_game = function(data_in)
        if nil ~= data_in and nil ~= data_in.game_id then
            local game_id = tonumber(data_in.game_id); 
            if nil ~= game_id and GAME_ID.INVALID_GAME_ID ~= game_id then
                wegame_mainframe_model.start_for_module_id = game_id;
                wegame_mainframe_model.has_start_for = true;
                wegame_mainframe_model.is_start_for_login = true;
                wegame_mainframe_control.first_page_param = 
                    wegame_mainframe_control.get_first_page_param();
                wegame_mainframe_control.first_focus_param = 
                    wegame_mainframe_control.first_page_param;
                local info = wegame_mainframe_control.get_first_page_background(
                    wegame_mainframe_control.first_page_param);
                if info ~= nil then
                    wegame_mainframe_view.web_notify_to_set_background(info.background_image, 
                        info.background_color, info.image_position);
                end

            end
        end
    end;

    save_main_wnd_pos = function ()
        if wegame_mainframe_view.e_framestate.min == wegame_mainframe_view.framestate or 
            true == wegame_mainframe_view.is_closed then
            -- 最小化和关闭状态尺寸有异常，不保存
            return;
        end
        wegame_mainframe_control.inner_log(string.format("save_main_wnd_pos, width:%d, height:%d",
            wegame_mainframe_model.last_wnd_size.width, wegame_mainframe_model.last_wnd_size.height));
        local width_info = {
            common = 0, key = "wegame_mainframe.wnd_width",
            value = wegame_mainframe_model.last_wnd_size.width};
        comm_center.call_service("Svr_SetConfig", width_info);
        local height_info = {
            common = 0, key = "wegame_mainframe.wnd_height",
            value = wegame_mainframe_model.last_wnd_size.height};
        comm_center.call_service("Svr_SetConfig", height_info);
        local is_max = false;
        if wegame_mainframe_view.e_framestate.max == wegame_mainframe_view.framestate then
            is_max = true;
        end
        local is_max_info = {
            common = 0, key = "wegame_mainframe.wnd_max_status",
            value = is_max};
        comm_center.call_service("Svr_SetConfig", is_max_info);
    end;


    on_downloader_tips = function(event_id, str)
        local data = base_utility.json_to_table(str);
        wegame_mainframe_control.downloader_tips_type = data.tips_type;
        wegame_mainframe_control.downloader_game_name = data.game_name;
        wegame_mainframe_control.downloader_game_id = data.game_id;
        wegame_mainframe_control.notify_downloader_tips_event();
    end;

    notify_downloader_tips_event = function()
        if (wegame_mainframe_control.downloader_tips_type ~= nil
            and wegame_mainframe_control.downloader_tips_type ~= ""
            and wegame_mainframe_control.downloader_game_name ~= nil
            and wegame_mainframe_control.downloader_game_name ~= "") then
            local callback_func = web_register_center.get_func_by_browser(
                "downloader_tips_event", CID_MAINFRAME_QBWEB);
            if callback_func ~= nil and callback_func ~= "" then
                local rsp_data_table = web_utility.notify_event_comm_data("downloader_tips_event");
                rsp_data_table["retCode"] = 0;
                rsp_data_table["msg"] = "";
                if rsp_data_table["data"] ~= nil then
                    rsp_data_table["data"]["tips_type"] = wegame_mainframe_control.downloader_tips_type;
                    rsp_data_table["data"]["game_name"] = wegame_mainframe_control.downloader_game_name;
                    rsp_data_table["data"]["game_id"] = wegame_mainframe_control.downloader_game_id;
                else
                    rsp_data_table["data"] = {tips_type=wegame_mainframe_control.downloader_tips_type,
                        game_name=wegame_mainframe_control.downloader_game_name,
                        game_id=wegame_mainframe_control.downloader_game_id};
                end
                web_utility.lua_call_js_ex(callback_func,
                        base_utility.table_to_json(rsp_data_table), CID_MAINFRAME_QBWEB);
                wegame_mainframe_control.downloader_game_name = "";
                wegame_mainframe_control.downloader_tips_type = "";
                wegame_mainframe_control.downloader_game_id = 0;
            end
        end
    end;

    load_resource = function()
        wegame_mainframe_control.inner_log("load_resource begin.");
        if wegame_mainframe_control.has_already_navigate then
            wegame_mainframe_control.inner_log("load_resource:has_already_navigate.");
            return;
        end
        wegame_mainframe_control.check_first_focus_gameid();
        local is_hidle_quick_login = false;
        local login_mgr =
            base_utility.get_component_adapter("ILogin_mgr",
                "ierd_tgp.login_mgr.GetLoginMgr();");
        if login_mgr ~= nil then
            is_hidle_quick_login = login_mgr:IsHideQuickLogin();
        end
        if not is_hidle_quick_login then
            wegame_mainframe_view.restore_layout();
            wegame_mainframe_control.has_already_navigate = true;
            -- 读取页面缓存开关
            local page_cache_key = {common = 1, key = "CfgMgr.UserSysSetting.PageCacheSetting"};
            local cache_data = comm_center.call_service("Svr_GetConfig", page_cache_key);
            if cache_data ~= nil and cache_data.exits == 1
                and cache_data.value ~= nil and cache_data.value ~= "" then
                 wegame_mainframe_model.page_cache_switch = tonumber(cache_data.value);
            end
        end
    end;

    -- 暂时保留
    set_jump_module_web_json = function (json_str)
        wegame_mainframe_control.inner_log("set_jump_module_web_json");
        wegame_mainframe_model.jump_module_web_json = json_str;
    end;

    set_login_srv_state = function (event_id, json_str)
        local info = base_utility.json_to_table(json_str);
        wegame_mainframe_model.login_srv_state = info.login_succ;
        wegame_mainframe_view.set_login_srv_state_mark();
        wegame_mainframe_control.web_notify_is_login_ = true;
        wegame_mainframe_control.web_notify_to_init();
        --MessageBox("set_login_srv_state");
    end;

    web_notify_to_init = function ()
        if wegame_mainframe_control.web_notify_is_done_ then
            return;
        end
        if wegame_mainframe_control.web_notify_is_login_ and wegame_mainframe_control.web_notify_is_register_event_ then
            wegame_mainframe_view.web_notify_to_init();
            wegame_mainframe_control.web_notify_is_done_ = true;
            --MessageBox("done");
        end
    end;

    handle_close_main_wnd_oper = function (data)
        wegame_mainframe_model.exit_when_close = data.close_main_wnd_oper;
    end;

    on_get_json_cfg = function(event_id, json_str)
       wegame_mainframe_control.inner_log("on_get_json_cfg");

        local config_table = base_utility.json_to_table(json_str);
        if config_table == nil then
            return;
        end;

        if config_table["json"] ~= nil and type(config_table["json"]) == 'table' 
            and config_table["json"]["data"] ~= nil then
            if config_table["name"] == "defualt_open_page_setting" then
                wegame_mainframe_control.inner_log("[on_get_json_cfg] defualt_open_page_setting");
                wegame_mainframe_control.handle_defualt_open_page_setting(
                    config_table["json"]["data"]);
            elseif config_table["name"] == "navigate_tab_menu_setting" then
                wegame_mainframe_control.inner_log("[on_get_json_cfg] navigate_tab_menu_setting");
                wegame_mainframe_control.handle_navigate_tab_menu_setting(
                    config_table["json"]["data"]);
            elseif config_table["name"] == "top_bar_skin" then 
                wegame_mainframe_control.inner_log("[on_get_json_cfg] top_bar_skin");
                wegame_mainframe_control.handle_top_bar_skin_setting(
                    config_table["json"]["data"]);
            elseif config_table["name"] == "preupdate_rule" then
                wegame_mainframe_control.inner_log("[on_get_json_cfg]preupdate_rule");
                wegame_mainframe_control.handle_preupdate_rule_setting(
                    config_table["json"]["data"]);
            elseif config_table["name"] == "app_tips_config" then
                wegame_mainframe_control.inner_log("[on_get_json_cfg]app_tips_config");
                wegame_mainframe_control.handle_app_tips_config(
                    config_table["json"]["data"]);
            end
        end
    end;

    handle_defualt_open_page_setting = function(data_table)
        base_utility.log("[wegame_mainframe_control]handle_defualt_open_page_setting");
        if nil ~= data_table then
            wegame_mainframe_model.default_open_page_setting = data_table[1];
            if false == wegame_mainframe_model.is_start_for_login then
                if base_utility.is_netbar_ver() then
                    wegame_mainframe_model.current_select_modure_id = 
                        tonumber(wegame_mainframe_model.default_open_page_setting.netbar_module_id);
                else
                    wegame_mainframe_model.current_select_modure_id = 
                        tonumber(wegame_mainframe_model.default_open_page_setting.module_id);
                end
            end
        end
    end;

    handle_navigate_tab_menu_setting = function (data_table)
        base_utility.log("[wegame_mainframe_control]handle_navigate_tab_menu_setting");
        if nil ~= data_table then
            -- wegame_mainframe_model.navigate_tab_menu_setting = data_table;
            for k, v in pairs(data_table) do
                if nil ~= v and nil ~= v.module_id then
                    wegame_mainframe_model.navigate_tab_menu_setting[v.module_id] = v;
                end
            end
        end
    end;

    handle_focus_item = function (data)
        if nil == data.game_id then
            return;
        end

        if nil ~= data.game_dir and "" ~= data.game_dir then
            wegame_mainframe_control.inner_log(string.format("game_dir:%s", data.game_dir));
            local data = {game_id = data.game_id, game_path = data.game_dir};
            comm_center.call_service("Svr_AddGame", data);    
        end
        local is_main_wnd_close = wegame_mainframe_view.is_closed;
        event_center.send_event(mainframe_event.SHOW_MAIN_WND, "");   
        if true == is_main_wnd_close then
            base_utility.remove_interval("wegame_mainframe_control.handle_delay_focus_itme", tonumber(data.game_id));
            base_utility.add_interval("wegame_mainframe_control.handle_delay_focus_itme", 1000, tonumber(data.game_id));
        else
            wegame_mainframe_control.focus_item(data.game_id);
        end
    end;

    handle_delay_focus_itme = function (user_data)
        if nil == user_data then
            return;
        end
        base_utility.remove_interval("wegame_mainframe_control.handle_delay_focus_itme", tonumber(user_data));
        wegame_mainframe_control.focus_item(user_data);
    end;

    focus_item = function (game_id)
        local event_table = {module_id = tonumber(game_id)};
        event_center.send_event(banner_event.JUMP_MODULE_WEB, base_utility.table_to_json(event_table)); 
    end;

    handle_req_is_vip = function ()
        local data = {};
        if true == wegame_mainframe_model.is_self_vip then
            data["is_vip"] = 1;
        else
            data["is_ivp"] = 0;
        end

        return data;
    end;

    handle_show_user_guide = function(param)
        if param == nil or param.focus_home_banner == nil or param.focus_home_banner == false then
            return;
        end
        -- 定位到首页
        wegame_mainframe_control.focus_item(GAME_ID.GAEM_ID_HOME_PAGE);
    end;

    get_current_mode = function ()
        local out = {mode = 0};
        return out;
    end;

    get_tgp_level = function(data_in)
        local user_level = 0;
        local user_game_info = ierd_tgp.users_info.User_game_info:new_local();
        local users_info_adapter =
            base_utility.get_component_adapter("IUsers_info_module",
                "ierd_tgp.users_info.GetUsersInfoModuleAdapter();");
        if nil ~= users_info_adapter then
            users_info_adapter:get_self_game_info(user_game_info);
            user_level = user_game_info.user_level;
        end
        return {tgp_level = user_level};
    end;

    launch_web_use_qb_browser = function(event_id, json_str)
        wegame_mainframe_control.inner_log("[main_control]launch_web_use_qb_browser, json_str: " .. json_str);
        event_center.send_event(browser_event.OPEN_WEB_GAME, json_str);
    end;

    -- 拉起守护进程后退出wegame主进程 -- 2017/09/22 maxyao
    auto_exit_main_process = function(data)
        event_center.send_event(main_web_event.TGP_CLOSE, "{}");
        event_center.send_event(group_event.TGP_CLOSE, "{}");
        local gamelib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        layout_mgr.hide_all_window();
        gamelib:do_exit_app();
    end;

    -- 游戏列表展开/收起逻辑
    process_game_list_pack_up = function ()
        wegame_mainframe_control.inner_log("process_game_list_pack_up", true);

        local game_id = nil;

        local launcher_info = comm_center.call_service("Srv_GetLauncherInfo", {});
        if nil ~= launcher_info and nil ~= launcher_info.info and "" ~= launcher_info.info then
             wegame_mainframe_control.inner_log(string.format("[process_game_list_pack_up]info:%s",
                                                launcher_info.info), true);
            local info_tb = base_utility.json_to_table(launcher_info.info);
            if nil ~= info_tb then 
                game_id = info_tb.game_id;
            end
        end
        
        if nil == game_id then
             local multi_launch_info = comm_center.call_service("Svr_GetMultiWeGameLaunchGameID", {});
             if nil ~= multi_launch_info and nil ~= multi_launch_info.game_id
                  and 0 ~= multi_launch_info.game_id then
                    game_id = multi_launch_info.game_id; 
             end
        end

        if nil == game_id then
            wegame_mainframe_control.inner_log("[process_game_list_pack_up]game_id is nil", true);
            return false;
        else
            wegame_mainframe_control.inner_log(string.format("[process_game_list_pack_up]game_id:%s", tostring(game_id)), true);
            wegame_mainframe_model.launcher_start_game = game_id;
            wegame_mainframe_view.set_pack_up_btn_backgroup(wegame_mainframe_model.launcher_start_game);
            if true == wegame_mainframe_model.is_pack_up_game_list(game_id) then
                wegame_mainframe_view.pack_up_game_list(true);
            else
                wegame_mainframe_view.pack_up_game_list(false);
            end
            wegame_mainframe_view.adjust_helper_bottom_bar_size();
            return true;
        end
    end;

    handle_web_move_animation_end = function (json)
        local param_table = base_utility.json_to_table(json);
        local move_type = nil;
        if nil ~= param_table and nil ~= param_table.data then
            move_type = param_table.data.move_type;
        end

        if nil == move_type then
            return;
        end
        wegame_mainframe_view.pack_up_list_animation_end(move_type);
    end;

    handle_bottom_bar_show = function (data)
        if nil == data or nil == data.game_id then
            return;
        end
        wegame_mainframe_view.set_pack_up_btn_backgroup(data.game_id);
    end;

    handle_frame_load_complete = function ()
        if true == wegame_mainframe_model.is_game_list_pack_up then
            -- 通知页面进行收起
            wegame_mainframe_view.web_move_animation(2);
        end
    end;


    handle_top_bar_skin_setting = function (data_table)
        base_utility.log("[wegame_mainframe_control]handle_top_bar_skin_setting");
        if nil ~= data_table then
            local data = data_table[1];
            wegame_mainframe_view.set_title_operation_skin(
                tonumber(data.is_show), data.skin_img, data.bkg_color, data.drop_img);
        end
    end;

    start_waiting_web_mainframe = function()
        wegame_mainframe_control.inner_log("start_waiting_web_mainframe");
        base_utility.remove_interval("wegame_mainframe_control.handle_waiting_web_mainframe");
        base_utility.add_interval("wegame_mainframe_control.handle_waiting_web_mainframe", 2000);
    end;

    handle_waiting_web_mainframe = function()
        wegame_mainframe_model.wait_times = wegame_mainframe_model.wait_times + 1;
        if wegame_mainframe_model.web_mainframe_ready or wegame_mainframe_model.wait_times > 2 then
            wegame_mainframe_control.inner_log("handle_waiting_web_mainframe : "..
                                               tostring(wegame_mainframe_model.wait_times)..", "..
                                               tostring(wegame_mainframe_model.web_mainframe_ready));

            base_utility.remove_interval("wegame_mainframe_control.handle_waiting_web_mainframe");

            local   zParam = {};
            zParam.isfirst = wegame_mainframe_model.first_time;

            wegame_mainframe_model.first_time = 0;
            wegame_mainframe_model.wait_times = 0;
            comm_center.send_broadcast("Msg_MainFrameShowFinish", zParam);
        end;
    end;

    handle_preupdate_rule_setting = function (data_table)
        base_utility.log("[wegame_mainframe_control]handle_preupdate_rule_setting");
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if nil == game_lib then
            return;
        end;

        if nil ~= data_table then
            for k, v in pairs(data_table) do
                if nil ~= v and nil ~= v.game_id and nil ~= v.rule_data then
                    local data_json_tb = base_utility.json_to_table(v.rule_data);
                    local key = string.format("preupdate_rule_%s", tonumber(v.game_id));
                    local rule_query_info = {common = 0, key = key};
                    local rule_data = comm_center.call_service("Svr_GetConfig", rule_query_info);
                    -- local game_state_data = comm_center.call_service("SrvGetGameState", {game_id = tonumber(v.game_id)});
                    local auto_update_setting = comm_center.call_service("Svr_GetAutoUpdateSetting", {game_id = tonumber(v.game_id)});
                    local is_reset = false;
                    local setting_val = wegame_mainframe_control.get_preupdate_setting_val(auto_update_setting.auto_update_setting, v.rule_data);
                    if 0 == rule_data.exits then
                        -- 不存在，使用新的配置
                        if nil ~= auto_update_setting and nil ~= auto_update_setting.auto_update_setting 
                            and auto_update_setting.auto_update_setting ~= setting_val then
                                base_utility.log(string.format("[wegame_mainframe_control]handle_preupdate_rule_setting,setting preupdate value:%d,game_id:%d",
                                    tonumber(setting_val), tonumber(v.game_id)));
                                game_lib:set_auto_update_game(tonumber(v.game_id), setting_val);
                                is_reset = true;
                        end
                        wegame_mainframe_model.preupdate_rule_open[tonumber(v.game_id)] = true;
                    else
                        local save_data_json_tb = base_utility.json_to_table(rule_data.value);
                        if nil == data_json_tb or nil == save_data_json_tb then
                            return;
                        end
                        if tonumber(data_json_tb.rule_id)  > tonumber(save_data_json_tb.rule_id) then
                            if nil ~= auto_update_setting and nil ~= auto_update_setting.auto_update_setting 
                                and auto_update_setting.auto_update_setting ~= setting_val then
                                    base_utility.log(string.format("[wegame_mainframe_control]handle_preupdate_rule_setting,setting preupdate value:%d,game_id:%d",
                                        tonumber(setting_val), tonumber(v.game_id)));
                                    game_lib:set_auto_update_game(tonumber(v.game_id), setting_val);
                                    is_reset = true;
                            end
                            wegame_mainframe_model.preupdate_rule_open[tonumber(v.game_id)] = true;
                        end
                    end
                    if true == is_reset then
                        local rule_save_info = {common = 0, key = key, value = v.rule_data};
                        comm_center.call_service("Svr_SetConfig", rule_save_info);
                    end
                end
            end
        end
    end;

    get_preupdate_setting_val = function (cur_val, rule_data_str)
        base_utility.log(string.format("[wegame_mainframe]get_preupdate_setting_val,cur_val:%s,rule_dta_str:%s",
            tostring(cur_val), tostring(rule_data_str)));
        local rule_data_tb = base_utility.json_to_table(rule_data_str);
        if nil == rule_data_tb or nil == rule_data_tb.source_val or nil == rule_data_tb.target_val then
            return cur_val;
        end
        local source_val1;
        local source_val2;
        local setting_val = cur_val;
        local target_val = tonumber(rule_data_tb.target_val);
        local index = string.find(rule_data_tb.source_val, "|");
        if nil == index then
            source_val1 = tonumber(rule_data_tb.source_val);
            if source_val1 == cur_val then
                setting_val = target_val;
            end
        else
            local source_tb = base_utility.split(rule_data_tb.source_val, "|");
            source_val1 = tonumber(source_tb[1]);
            source_val2 = tonumber(source_tb[2]);
            if source_val1 == cur_val or source_val2 == cur_val then
                setting_val = target_val;
            end
        end

        return setting_val;
    end;

    get_preupdate_rule_open = function (data)
        if nil == data then
            return;
        end
        local is_open = wegame_mainframe_model.preupdate_rule_open[data.game_id];
        if true == is_open then
            data["is_open"] = 1;
        else
            data["is_open"] = 0;
        end

        return data;
    end;

    on_common_cfg_updated = function ()
       -- 配置更新，则再尝试查询
       wegame_mainframe_control.inner_log("[on_common_cfg_updated]");
       wegame_mainframe_control.query_fn_authority();
    end;

    query_fn_authority = function ()
        if false == wegame_mainframe_model.is_fn_swtich_open() then
            -- 开关关闭，尝试从游戏列表里面移除fn
            base_utility.inner_log("[add_fn_to_my_game_list]fn switch is not open");
            wegame_mainframe_model.delete_fn_from_game_list();
            return;
        end

        if true == wegame_mainframe_model.has_get_game_authority then
            -- 已经拉取过了，不重复拉取
            base_utility.inner_log("[add_fn_to_my_game_list]has query");
            return;
        end

        if false == wegame_mainframe_model.is_add_fn_to_game_list() then
            -- 配置为不添加，则直接返回
            base_utility.inner_log("[add_fn_to_my_game_list]fn has add to game list");
            return;
        end

        if true == wegame_mainframe_model.is_in_my_game_list(GAME_ID.GAME_ID_FN) then
            -- 已经在游戏列表中了，返回
            base_utility.inner_log("[add_fn_to_my_game_list]already in game list");
            return;
        end
        wegame_mainframe_model.get_game_authority(GAME_ID.GAME_ID_FN);
    end;

    handle_change_region_succ_notify = function()
        comm_center.call_service("Svr_ReqBizTicketFromServer", {});
        wegame_mainframe_control.bNeedNotifyWebRefreshPage = true;
    end;

    handle_userself_region_notify = function()
        if wegame_mainframe_control.bNeedNotifyWebRefreshPage then
            comm_center.call_service("SvrRefreshWGIFrame", {});
            wegame_mainframe_control.bNeedNotifyWebRefreshPage = false;
        end     
    end;

    handle_blacklist_user_notify = function()
        wegame_mainframe_model.is_blacklist_user = true;
    end;

    handle_get_current_nav_tab = function()
        local data = {};
        data["game_id"] = wegame_mainframe_model.current_select_modure_id;
        return data;
    end;


    handle_app_tips_config = function (data_table)
        base_utility.log("[wegame_mainframe_control]handle_app_tips_config");
        if nil ~= data_table then
            local data = data_table[1];
            wegame_mainframe_view.set_app_tips_config(data);
        end
    end;

    handle_page_cache_switch_change = function (data)
        local cache_switch = 0;
        if data ~= nil and data.cache_switch ~= nil then
            cache_switch = data.cache_switch;
        end
        if cache_switch == wegame_mainframe_model.page_cache_switch then
            return
        end
        -- 保存设置
        wegame_mainframe_model.page_cache_switch = cache_switch;
        local callback_func = web_register_center.get_func_by_browser(
            "web_mainframe_comm_listen", CID_MAINFRAME_QBWEB);
        if callback_func ~= nil and callback_func ~= "" then
            local cache_info = {};
            cache_info["cache_switch"] = cache_switch;
            local rsp_data_table = 
                web_utility.notify_event_comm_data("web_mainframe_comm_listen");
            rsp_data_table.data.data = cache_info;
            rsp_data_table.data.custom_cmd = "update_page_cache_state";
            web_utility.lua_call_js_ex(callback_func, 
                base_utility.table_to_json(rsp_data_table), CID_MAINFRAME_QBWEB);
        end
    end;

    on_load_page_complete = function ()
        wegame_mainframe_view.on_load_page_complete();
    end;

    on_mygame_list_update = function()
        wegame_mainframe_control.mygame_list_ready = true;
    end;

    query_user_org_guide_config = function()
        local param = {
            reqfrom = "pc"
        };
        local url = LoadStr("SOCIAL_ORG_GUIDE_CONFIG_URL");
        base_utility.curl_request_async (url,
            "wegame_mainframe_control.on_query_org_guide_config", false, nil, true, nil,
            base_utility.table_to_json(param));
    end;

    on_query_org_guide_config = function(rsp)
        rsp = rsp or "{}";
        base_utility.log("[org_guide_cfg]rsp: " .. rsp);
        if "" == rsp then
            return;
        end
        local rsp_tbl = base_utility.json_to_table(rsp);
        if nil == rsp_tbl or 0 ~= rsp_tbl.result then
            return;
        end

        wegame_mainframe_control.new_social_init_org_list.need_guide_page = rsp_tbl.need_guide_page;

        if pwg_social_user_guide.kGuidePageRecommendOrg == rsp_tbl.need_guide_page then
            if nil == rsp_tbl.org_list then
                return;
            end
            wegame_mainframe_control.new_social_init_org_list.org_list = rsp_tbl.org_list;
            base_utility.log("[org_guide_cfg]show recommend page", true);
            base_utility.add_interval("wegame_mainframe_control.show_new_social_guide", 1000);
        elseif pwg_social_user_guide.kGuidePageNewSocial == rsp_tbl.need_guide_page then
            base_utility.log("[org_guide_cfg]show guide page", true);
            base_utility.add_interval("wegame_mainframe_control.show_new_social_guide", 1000);
        end
    end;

    show_new_social_guide = function()
        -- try show
        local guide_type = wegame_mainframe_control.new_social_init_org_list.need_guide_page or 0;
        if guide_type == pwg_social_user_guide.kGuidePageRecommendOrg then
            local ret = comm_center.call_service("i_service_query_simple_user_guide_show", {});
            if nil ~= ret and 1 == ret.result then
                base_utility.remove_interval("wegame_mainframe_control.show_new_social_guide");
                local params = {
                    url = LoadStr("SOCIAL_GUIDE_URL1");
                    width = pwg_social_user_guide.kGuidePageWidth;
                    height = pwg_social_user_guide.kGuidePageHeight;
                    always_on_top = true;
                    hide_close_btn = true;
                    caption = LoadStr("PWG_SOCIAL_CAPTION");
                };
                comm_center.call_service("i_service_show_simple_web_window", params);
            end
        elseif guide_type == pwg_social_user_guide.kGuidePageNewSocial then
            local params = {
                guide_url = LoadStr("SOCIAL_GUIDE_URL2");
                guide_type = "new_social_guide";
            };
            local ret = comm_center.call_service("i_service_show_simple_user_guide", params);
            if nil ~= ret and 1 == ret.result then
                base_utility.remove_interval("wegame_mainframe_control.show_new_social_guide");
            end
        else
            base_utility.log("[org_guide_cfg]unknown guid type " .. tostring(guide_type), true);
            base_utility.remove_interval("wegame_mainframe_control.show_new_social_guide");
        end
    end;

    -- 页面显示后（保证此时数据已请求）查询
    query_new_social_init_org_list_svr = function()
        local result = wegame_mainframe_control.new_social_init_org_list or {};
        return result;
    end;

    on_check_pwg_social_state = function()
        local result = { is_enabled = wegame_mainframe_control.pwg_social_enabled };
        return result;
    end;

    init_destroy_strat = function()
        local data = comm_center.call_service("i_service_check_page_preload", {});
        if data ~= nil and data.open == true then
            base_utility.log("init_destroy_strat keep browser alive", true);
            wegame_mainframe_control.destroy_when_close_wnd_ = false;
        end;
    end;

    on_color_mode_update = function(data)
        if nil == data or nil == data.color_mode then
            return;
        end
        base_utility.log("[mainframe]on_color_mode_update: " .. tostring(data.color_mode), true);
        wegame_mainframe_control.switch_color_mode(data.color_mode);
    end;

    switch_color_mode = function(color_mode)
        base_utility.log("[mainframe]switch_color_mode: " .. tostring(color_mode), true);
        if wegame_mainframe_control.cur_color_mode_~= nil and
            wegame_mainframe_control.cur_color_mode_ == color_mode then
            return;
        end;

        wegame_mainframe_control.cur_color_mode_ = color_mode;
        -- TODO(amadeusyu): change tpf style
        local mainframe_id = ui_utility.get_ctrl_id_by_cid(CID_MAINFRAME_Dialog_MainFrame);
        if mainframe_id == nil then
            return;
        end;
        local bottom_dlg_id = ui_utility.get_ctrl_id_by_path(mainframe_id,
                           "pnlClient\\Ctn_BottomBar\\Dlg_Bottom");
         if bottom_dlg_id == nil then
            base_utility.log("[mainframe]switch_color_mode no dlg_bottom", true);
            return;
         end;
        local bottom_dlg_ptr = ui_utility.get_ctrl_ptr_by_id(bottom_dlg_id);
        local uilib = ui_utility.uilib();
        if bottom_dlg_ptr == nil or uilib == nil then
            base_utility.log("[mainframe]switch_color_mode no dlg_bottom ptr", true);
            return;
        end;
        local sText;
        if color_mode == MainCommon.color_mode.kLight then
            sText = "light";
        else
            sText = "dark";
        end;
        local const_ui_str2, str_holder2 = U8toCUIStr(sText);
        uilib:ChangeSkinMode(const_ui_str2);
        ui_utility.setproperty_by_id(bottom_dlg_id, "SkinMode", color_mode);
    end;
};

wegame_mainframe_control.init();